package com.inspirationandgenius.welcome;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import org.xmlpull.v1.XmlPullParser;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.lifeofdata.inspirationandgenius.R;
import com.lifeofdata.inspirationandgenius.R.layout;

public class ControlTower extends ListActivity implements android.view.View.OnClickListener{

	private Intent intent; 
	
	DBAdapter db;
	private Button btn;
	
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);

	        db = new DBAdapter(this);
	        db.open();
	        
        	Cursor onlineigUsers= db.getOnline_igUsers();
	        startManagingCursor(onlineigUsers);
	        
	        if(onlineigUsers.getCount()==0)
	        {
	        	
	        	setContentView(R.layout.login);
	        	Button igusersadd = (Button)findViewById(R.id.igusersadd);
	        	igusersadd.setOnClickListener(this);
	        	DataBind();
	        	
	        	ListView lv = getListView();
	        	lv.setOnItemClickListener(new OnItemClickListener() {
	        		 
	                public void onItemClick(AdapterView<?> parent, View view,
	                        int position, long id) {
	                	
	               final String iduser = ((TextView) view.findViewById(R.id.iduser)).getText()
	                            .toString();
	               final String idlog = ((TextView) view.findViewById(R.id.iduserlog)).getText()
                           .toString();
		               ImageView td = (ImageView)findViewById(R.id.userimage);
		               td.setDrawingCacheEnabled(true);
		               td.buildDrawingCache();
		               Bitmap bitmap =td.getDrawingCache();
	            	   	Intent ilog = new Intent(getApplicationContext(), Login.class);
	            	   	ilog.putExtra("id", iduser);
	            	   	ilog.putExtra("image", bitmap);
	            	   	ilog.putExtra("login", idlog);
	            	   	startActivityForResult(ilog, 20091990);
	            	   	finish();
	                }
	                });
	        	}
	        else
	        	{
	        		setContentView(R.layout.testing);
	        		btn = (Button)findViewById(R.id.disconnectall);
	        		btn.setOnClickListener(this);
	        		
	        	}
	        
		}
	 
	 public void DataBind()
	 {
		 Cursor v = db.getAlligUsers();
		 startManagingCursor(v);
		 
		 DomainAdapter databaseListAdapter = new DomainAdapter(this, 
	                R.layout.login, 
	                v, 
	                new String[] {"_id","name","login", "email"}, 
	                new int[] { R.id.iduser,R.id.name,R.id.iduserlog, R.id.email });
	databaseListAdapter.notifyDataSetChanged();
	setListAdapter(databaseListAdapter);
	db.close();
		
	 }

	public void onClick(View v) {
		
		switch(v.getId())
		{
		
			case R.id.igusersadd:
				intent = new Intent(this, Inscription.class);
				this.startActivity(intent);
				finish();
				break;
				
			case R.id.disconnectall:
				db.DisconnectAll_igUsers();
				db.close();
				Intent ite = new Intent(getApplicationContext(), ControlTower.class);
				startActivity(ite);
				finish();
				break;
			default: break;
		}
			
	}
	
	 
	 
	 @Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		db.close();
	}
	
	 
	 public class DomainAdapter extends SimpleCursorAdapter{
		    private Cursor dataCursor;

		    private LayoutInflater mInflater;
		    
		    
		    
		    public DomainAdapter(Context context, int layout, Cursor dataCursor, String[] from,
		            int[] to) {
		        super(context, layout, dataCursor, from, to);
		            this.dataCursor = dataCursor;
		            mInflater = LayoutInflater.from(context);
		    }

		    public View getView(int position, View convertView, ViewGroup parent) {
		       
		        ViewHolder holder;

		        
		        if (convertView == null) {
					convertView = mInflater.inflate( R.layout.list_of_user , null);

		            
		            holder = new ViewHolder();
		            holder.text1 = (TextView) convertView.findViewById(R.id.name);
		            holder.text2 = (TextView) convertView.findViewById(R.id.email);
		            holder.icon = (ImageView) convertView.findViewById(R.id.userimage);
		            holder.text3 = (TextView) convertView.findViewById(R.id.iduserlog);
		            holder.text4 = (TextView) convertView.findViewById(R.id.iduser);

		            convertView.setTag(holder);
		        } else {
		           
		            holder = (ViewHolder) convertView.getTag();
		        }

		        
		        dataCursor.moveToPosition(position);
		        int title_index = dataCursor.getColumnIndex("name"); 
		        String title = dataCursor.getString(title_index);

		        int description_index = dataCursor.getColumnIndex("email"); 
		        String description = dataCursor.getString(description_index);
		        
		        byte[] image =dataCursor.getBlob(dataCursor.getColumnIndex("image"));
		     	ByteArrayInputStream imageStream = new ByteArrayInputStream(image);
		   		 Bitmap theImage= BitmapFactory.decodeStream(imageStream);
		   		
		   		int usid = dataCursor.getColumnIndex("_id");
		   		String usidd=dataCursor.getString(usid);
		   		
		   		int uslog = dataCursor.getColumnIndex("login");
		   		String uslogin=dataCursor.getString(uslog);
		   		 
		        holder.text1.setText(title);
		        holder.text2.setText(description);
		        holder.text3.setText(uslogin);
		        holder.text4.setText(usidd);
		        holder.icon.setImageBitmap(theImage);
		       

		        return convertView;
		    }

		   
		}
	 
	 public static class ViewHolder {
	        TextView text1;
	        TextView text2;
	        ImageView icon;
	        TextView text3;
	        TextView text4;
	    }
	
}
